<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="林湃滨的个人博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    【Mybatis】resultMap解析 |  滨书
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="滨书" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Mybatis中的resultMap解析" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  【Mybatis】resultMap解析
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/01/15/Mybatis%E4%B8%AD%E7%9A%84resultMap%E8%A7%A3%E6%9E%90/" class="article-date">
  <time datetime="2020-01-15T14:02:06.000Z" itemprop="datePublished">2020-01-15</time>
</a>
      
      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.4k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">11分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="ResultMap解析"><a href="#ResultMap解析" class="headerlink" title="ResultMap解析"></a>ResultMap解析</h1><p>在Mybatis中，resultMap节点定义了结果集和结果对象（JavaBean）之间的映射规则。</p>
<p>本文章主要讲述的是resultMap的解析。</p>
<h2 id="相关基础类"><a href="#相关基础类" class="headerlink" title="相关基础类"></a>相关基础类</h2><h3 id="1、ResultMapping：列映射类"><a href="#1、ResultMapping：列映射类" class="headerlink" title="1、ResultMapping：列映射类"></a>1、ResultMapping：列映射类</h3><p>ResultMapping对象记录了结果集中的一列与对应JavaBean中一个属性的映射关系；</p>
<blockquote>
<p>文章参考：</p>
<p> <a href="https://www.cnblogs.com/homejim/p/9833863.html" target="_blank" rel="noopener">列映射类ResultMapping</a> </p>
</blockquote>
<h3 id="2、ResultMap：结果集映射类ResultMap"><a href="#2、ResultMap：结果集映射类ResultMap" class="headerlink" title="2、ResultMap：结果集映射类ResultMap"></a>2、ResultMap：结果集映射类ResultMap</h3><p>ResultMap对应的是结果集中的一个结果集。其基本组成部分中，含有ResultMapping对象。</p>
<blockquote>
<p>文章参考：</p>
<p><a href="https://www.cnblogs.com/homejim/p/9840373.html" target="_blank" rel="noopener">结果集映射类ResultMap</a></p>
</blockquote>
<h2 id="解析"><a href="#解析" class="headerlink" title="解析"></a>解析</h2><h3 id="入口函数"><a href="#入口函数" class="headerlink" title="入口函数"></a>入口函数</h3><p>resultMap是解析Mapper的一个环节，其处于mapper.xml文件下。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">resultMapElements(context.evalNodes(<span class="string">"/mapper/resultMap"</span>));</span><br></pre></td></tr></table></figure>

<p>解析是可以存在多个的，故 <code>context.evalNodes(&quot;/mapper/resultMap&quot;)</code>返回的是一个List列表。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">resultMapElements</span><span class="params">(List&lt;XNode&gt; list)</span><span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(XNode resultMapNode:list)&#123;</span><br><span class="line">        <span class="keyword">try</span>&#123;</span><br><span class="line">            resultMapElement(resultMapNode);</span><br><span class="line">        &#125;<span class="keyword">catch</span>(IncompleteElementException e)&#123;</span><br><span class="line">            <span class="comment">//ignore,it will be retried</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="resultMapElement函数"><a href="#resultMapElement函数" class="headerlink" title="resultMapElement函数"></a>resultMapElement函数</h3><p>整个过程都是在resultMapElement这个函数中进行的，相关代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> ResultMap <span class="title">resultMapElement</span><span class="params">(XNode resultMapNode)</span> <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> resultMapElement(resultMapNode,Collections,&lt;ResultMapping&gt; emptyList());</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 处理&lt;resultMap&gt;节点，将节点解析成ResultMap对象，下面包含有ResultMapping对象组成的列表</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> resultMapNode resultMap节点</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> additionalResultMappings 另外的ResultMapping列</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> ResultMap对象</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> ResultMap <span class="title">resultMapElement</span><span class="params">(XNode resultMapNode,List&lt;ResultMapping&gt; additionalResultMappings)</span> <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">    ErrorContext.instance().activity(<span class="string">"processing "</span>+resultMapNode.getValueBasedIdentifier());</span><br><span class="line">    <span class="comment">//获取Id，默认拼装所有父节点的id或value或property。是一个身份标识，具有唯一性。</span></span><br><span class="line">    String id = resultMapNode.getStringAttribute(<span class="string">"id"</span>,resultMapNode.getValueBasedIdentifier());</span><br><span class="line">    <span class="comment">//获取type属性，表示结果集将被映射为type指定类型的对象</span></span><br><span class="line">    String type = resultMapNode.getStringAttribute(<span class="string">"type"</span>,</span><br><span class="line">                                               resultMapNode.getStringAttribute(<span class="string">"ofType"</span>,</span><br><span class="line">                                           resultMapNode.getStringAttribute(<span class="string">"resultType"</span>,</span><br><span class="line">                                         resultMapNode.getStringAttribute(<span class="string">"javaType"</span>))));</span><br><span class="line">    <span class="comment">//获取extends属性，表示结果集的继承</span></span><br><span class="line">    String extend = resultMapNode.getStringAttribute(<span class="string">"extends"</span>);</span><br><span class="line">    <span class="comment">//自动映射属性。列名自动映射为属性</span></span><br><span class="line">    Boolean autoMapping = resultMapNode.getBooleanAttribute(<span class="string">"autoMapping"</span>);</span><br><span class="line">    <span class="comment">//解析type，获取其类型</span></span><br><span class="line">    Class&lt;?&gt; typeClass = resolveClass(type);</span><br><span class="line">    Discriminator discriminator = <span class="keyword">null</span>;</span><br><span class="line">    <span class="comment">//记录解析结果</span></span><br><span class="line">    List&lt;ResultMapping&gt; resultMappings = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    resultMappings.addAll(additionalResultMappings);</span><br><span class="line">    <span class="comment">//处理子节点</span></span><br><span class="line">    List&lt;XNode&gt; resultChildren = resultMapNode.getChildren();</span><br><span class="line">    <span class="keyword">for</span>(XNode resultChild : resultChildren)&#123;</span><br><span class="line">        <span class="comment">//处理constructor节点</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="string">"constructor"</span>.equals(resultChild.getName()))&#123;</span><br><span class="line">            <span class="comment">//解析构造函数元素，其下的每一个子节点产生一个ResultMapping对象</span></span><br><span class="line">            processConstructorElement(resultChild,typeClass,resultMappings);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="string">"discriminator"</span>.equals(resultChild.getName())) &#123;<span class="comment">//处理discriminator节点</span></span><br><span class="line">            discriminator = processDiscriminatorElement(resultChild,typeClass,resultMappings);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;<span class="comment">//处理其余节点</span></span><br><span class="line">            List&lt;ResultFlag&gt; flags = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">            <span class="keyword">if</span>(<span class="string">"id"</span>.equals(resultChild.getName()))&#123;</span><br><span class="line">                flags.add(ResultFlag.ID);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//创建resultMap对象，并添加到resultMappings中</span></span><br><span class="line">            resultMapping.add(buildResultMappingFromContext(resultChild,typeClass,flags));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 创建 ResultMapResolver 对象， 该对象可以生成 ResultMap 对象</span></span><br><span class="line">    ResultMapResolver resultMapResolver = <span class="keyword">new</span> ResultMapResolver(builderAssistant,id,typeClass,extend, discriminator, resultMappings, autoMapping);</span><br><span class="line">    <span class="keyword">try</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> resultMapResolver.resolve();</span><br><span class="line">    &#125;<span class="keyword">catch</span> (IncompleteElementException e)&#123;</span><br><span class="line">        <span class="comment">//若无法创建ResultMap对象，则将该结果添加到incompleteResultMaps集合中</span></span><br><span class="line">        configuration.addIncompleteResultMap(resultMapResolver);</span><br><span class="line">        <span class="keyword">throw</span> e;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="详细解析"><a href="#详细解析" class="headerlink" title="详细解析"></a>详细解析</h3><ul>
<li><p>获取id</p>
<p>id 对于 resultMap 来说是很重要的， 它是一个身份标识。具有唯一性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取 ID , 默认值会拼装所有父节点的 id 或 value 或 property。</span></span><br><span class="line">    String id = resultMapNode.getStringAttribute(<span class="string">"id"</span>, resultMapNode.getValueBasedIdentifier());</span><br></pre></td></tr></table></figure>

<p>这里涉及到 <code>XNode</code> 对象中的两个函数：getStringAttribute()以及getValueBasedIdentifier()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">getStringAttribute</span><span class="params">(String name, String def)</span> </span>&#123;</span><br><span class="line">    String value = attributes.getProperty(name);</span><br><span class="line">    <span class="keyword">if</span> (value == <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> def;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该函数是获取 <code>XNode</code> 对象对应 <code>XML</code> 节点的 <code>name</code> 属性值， 如果该属性不存在， 则返回传入的默认值 def。</p>
<p>而在获取 id 的过程中, 默认值是下面这个函数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 生成元素节点的基础 id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">getValueBasedIdentifier</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  StringBuilder builder = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">  XNode current = <span class="keyword">this</span>;</span><br><span class="line">  <span class="comment">// 当前的节点不为空</span></span><br><span class="line">  <span class="keyword">while</span> (current != <span class="keyword">null</span>) &#123;</span><br><span class="line">    <span class="comment">// 如果节点不等于 this， 则在0之前插入 _ 符号， 因为是不断的获取父节点的， 因此是插在前面</span></span><br><span class="line">    <span class="keyword">if</span> (current != <span class="keyword">this</span>) &#123;</span><br><span class="line">      builder.insert(<span class="number">0</span>, <span class="string">"_"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 获取 id， id不存在则获取value, value不存在则获取 property。</span></span><br><span class="line">    String value = current.getStringAttribute(<span class="string">"id"</span>,</span><br><span class="line">        current.getStringAttribute(<span class="string">"value"</span>,</span><br><span class="line">            current.getStringAttribute(<span class="string">"property"</span>, <span class="keyword">null</span>)));</span><br><span class="line">    <span class="comment">// value 非空， 则将.替换为_， 并将value的值加上 []</span></span><br><span class="line">    <span class="keyword">if</span> (value != <span class="keyword">null</span>) &#123;</span><br><span class="line">      value = value.replace(<span class="string">'.'</span>, <span class="string">'_'</span>);</span><br><span class="line">      builder.insert(<span class="number">0</span>, <span class="string">"]"</span>);</span><br><span class="line">      builder.insert(<span class="number">0</span>,</span><br><span class="line">          value);</span><br><span class="line">      builder.insert(<span class="number">0</span>, <span class="string">"["</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 不管 value 是否存在， 前面都添加上节点的名称</span></span><br><span class="line">    builder.insert(<span class="number">0</span>, current.getName());</span><br><span class="line">    <span class="comment">// 获取父节点</span></span><br><span class="line">    current = current.getParent();</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> builder.toString();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该函数是生成元素节点的id, 如果是这样子的 XML。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">employee</span> <span class="attr">id</span>=<span class="string">"$&#123;id_var&#125;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">blah</span> <span class="attr">something</span>=<span class="string">"that"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">first_name</span>&gt;</span>Jim<span class="tag">&lt;/<span class="name">first_name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">last_name</span>&gt;</span>Smith<span class="tag">&lt;/<span class="name">last_name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">birth_date</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">year</span>&gt;</span>1970<span class="tag">&lt;/<span class="name">year</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">month</span>&gt;</span>6<span class="tag">&lt;/<span class="name">month</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">day</span>&gt;</span>15<span class="tag">&lt;/<span class="name">day</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">birth_date</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">height</span> <span class="attr">units</span>=<span class="string">"ft"</span>&gt;</span>5.8<span class="tag">&lt;/<span class="name">height</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">weight</span> <span class="attr">units</span>=<span class="string">"lbs"</span>&gt;</span>200<span class="tag">&lt;/<span class="name">weight</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">active</span>&gt;</span>true<span class="tag">&lt;/<span class="name">active</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">employee</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>我们调用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">XNode node = parser.evalNode(<span class="string">"/employee/height"</span>); node.getValueBasedIdentifier();</span><br></pre></td></tr></table></figure>

<p>则， 返回值应该是</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">employee[$&#123;id_var&#125;]_height</span><br></pre></td></tr></table></figure>
</li>
<li><p>解析结果集中的类型</p>
<p> 结果集的类型， 对应的是一个 <code>JavaBean</code> 对象。通过反射来获得该类型。 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取type， type 不存在则获取 ofType, ofType </span></span><br><span class="line"><span class="comment">// 不存在则获取 resultType, resultType 不存在则获取 javaType</span></span><br><span class="line">String type = resultMapNode.getStringAttribute(<span class="string">"type"</span>,</span><br><span class="line">    resultMapNode.getStringAttribute(<span class="string">"ofType"</span>,</span><br><span class="line">        resultMapNode.getStringAttribute(<span class="string">"resultType"</span>,</span><br><span class="line">            resultMapNode.getStringAttribute(<span class="string">"javaType"</span>))));</span><br><span class="line"><span class="comment">// ... ...</span></span><br><span class="line"><span class="comment">// 获取 type 对应的 Class 对象</span></span><br><span class="line">Class&lt;?&gt; typeClass = resolveClass(type);</span><br></pre></td></tr></table></figure>

<p> 看源码， 有很多个 def 值， 也就是说， 我们在配置结果集的类型的时候都是有优先级的。但是， 这里有一个奇怪的地方， 我源代码版本（3.5.0-SNAPSHOT）的 的属性， 只有 type, 没有 ofType/resultType/javaType。以下为相应的 DTD 约束： </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!ELEMENT resultMap (constructor?,id*,result*,association*,collection*, discriminator?)&gt;</span><br><span class="line">&lt;!ATTLIST resultMap</span><br><span class="line">id CDATA #REQUIRED</span><br><span class="line">type CDATA #REQUIRED</span><br><span class="line">extends CDATA #IMPLIED</span><br><span class="line">autoMapping (true|false) #IMPLIED&gt;</span><br></pre></td></tr></table></figure>

<p>这里可能是考虑到对以前版本的兼容性。</p>
</li>
<li><p>获取继承结果集和自动映射</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String extend = resultMapNode.getStringAttribute(<span class="string">"extends"</span>);</span><br><span class="line">   Boolean autoMapping = resultMapNode.getBooleanAttribute(<span class="string">"autoMapping"</span>);</span><br></pre></td></tr></table></figure>

<p>这两个属性在配置XML的时候，显得有些可有可无。</p>
</li>
<li><p>解析</p>
<p>根据类型进行解析，获得resultMappings</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建一个 resultMappings 的链表</span></span><br><span class="line">   List&lt;ResultMapping&gt; resultMappings = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">   <span class="comment">// 将从其他地方传入的additionalResultMappings添加到该链表中</span></span><br><span class="line">   resultMappings.addAll(additionalResultMappings);</span><br><span class="line">   <span class="comment">// 获取子节点</span></span><br><span class="line">   List&lt;XNode&gt; resultChildren = resultMapNode.getChildren();</span><br><span class="line">   <span class="comment">// 遍历解析子节点</span></span><br><span class="line">   <span class="keyword">for</span> (XNode resultChild : resultChildren) &#123;</span><br><span class="line">     <span class="keyword">if</span> (<span class="string">"constructor"</span>.equals(resultChild.getName())) &#123;</span><br><span class="line">       <span class="comment">// 解析构造函数元素，其下的没每一个子节点都会生产一个 ResultMapping 对象</span></span><br><span class="line">       processConstructorElement(resultChild, typeClass, resultMappings);</span><br><span class="line">     &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"discriminator"</span>.equals(resultChild.getName())) &#123;</span><br><span class="line">       <span class="comment">// 解析 discriminator 节点</span></span><br><span class="line">       discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);</span><br><span class="line">     &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">       <span class="comment">// 解析其余的节点</span></span><br><span class="line">       List&lt;ResultFlag&gt; flags = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">       <span class="keyword">if</span> (<span class="string">"id"</span>.equals(resultChild.getName())) &#123;</span><br><span class="line">         flags.add(ResultFlag.ID);</span><br><span class="line">       &#125;</span><br><span class="line">       resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>除了discriminator节点，其他节点最后都会回到buildResultMappingFromContext方法，该方法为创建ResultMapping对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 获取一行， 如result等， 取得他们所有的属性， 通过这些属性建立 `ResultMapping` 对象</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> context 对于节点本身</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> resultType resultMap 的结果类型</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> flags flag 属性， 对应 ResultFlag 枚举中的属性。 一般情况下为空</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@return</span> 返回 ResultMapping</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">private</span> ResultMapping <span class="title">buildResultMappingFromContext</span><span class="params">(XNode context, Class&lt;?&gt; resultType, List&lt;ResultFlag&gt; flags)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    String property;</span><br><span class="line">    <span class="comment">// 获取节点的属性， 如果节点是构造函数（只有name属性， 没有property），</span></span><br><span class="line">    <span class="comment">// 则获取的是 name, 否则获取 property</span></span><br><span class="line">    <span class="keyword">if</span> (flags.contains(ResultFlag.CONSTRUCTOR)) &#123;</span><br><span class="line">      property = context.getStringAttribute(<span class="string">"name"</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      property = context.getStringAttribute(<span class="string">"property"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    String column = context.getStringAttribute(<span class="string">"column"</span>);</span><br><span class="line">    String javaType = context.getStringAttribute(<span class="string">"javaType"</span>);</span><br><span class="line">    String jdbcType = context.getStringAttribute(<span class="string">"jdbcType"</span>);</span><br><span class="line">    String nestedSelect = context.getStringAttribute(<span class="string">"select"</span>);</span><br><span class="line">    <span class="comment">// 获取嵌套的结果集</span></span><br><span class="line">    String nestedResultMap = context.getStringAttribute(<span class="string">"resultMap"</span>,</span><br><span class="line">        processNestedResultMappings(context, Collections.&lt;ResultMapping&gt; emptyList()));</span><br><span class="line">    String notNullColumn = context.getStringAttribute(<span class="string">"notNullColumn"</span>);</span><br><span class="line">    String columnPrefix = context.getStringAttribute(<span class="string">"columnPrefix"</span>);</span><br><span class="line">    String typeHandler = context.getStringAttribute(<span class="string">"typeHandler"</span>);</span><br><span class="line">    String resultSet = context.getStringAttribute(<span class="string">"resultSet"</span>);</span><br><span class="line">    String foreignColumn = context.getStringAttribute(<span class="string">"foreignColumn"</span>);</span><br><span class="line">    <span class="keyword">boolean</span> lazy = <span class="string">"lazy"</span>.equals(context.getStringAttribute(<span class="string">"fetchType"</span>, configuration.isLazyLoadingEnabled() ? <span class="string">"lazy"</span> : <span class="string">"eager"</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 以上获取各个属性节点</span></span><br><span class="line">    <span class="comment">// 解析 javaType， typeHandler， jdbcType</span></span><br><span class="line">    Class&lt;?&gt; javaTypeClass = resolveClass(javaType);</span><br><span class="line">    <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line">    Class&lt;? extends TypeHandler&lt;?&gt;&gt; typeHandlerClass = (Class&lt;? extends TypeHandler&lt;?&gt;&gt;) resolveClass(typeHandler);</span><br><span class="line">    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);</span><br><span class="line">    <span class="comment">// 创建resultMapping对象</span></span><br><span class="line">    <span class="keyword">return</span> builderAssistant.buildResultMapping(resultType, property, column, javaTypeClass, jdbcTypeEnum, nestedSelect, nestedResultMap, notNullColumn, columnPrefix, typeHandlerClass, flags, resultSet, foreignColumn, lazy);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>对于discriminator，则处理该元素，并创建鉴别器。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 处理鉴别器</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> context 节点</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> resultType 结果类型</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> resultMappings 列结果集合</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@return</span> 鉴别器</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">private</span> Discriminator <span class="title">processDiscriminatorElement</span><span class="params">(XNode context, Class&lt;?&gt; resultType, List&lt;ResultMapping&gt; resultMappings)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    String column = context.getStringAttribute(<span class="string">"column"</span>);</span><br><span class="line">    String javaType = context.getStringAttribute(<span class="string">"javaType"</span>);</span><br><span class="line">    String jdbcType = context.getStringAttribute(<span class="string">"jdbcType"</span>);</span><br><span class="line">    String typeHandler = context.getStringAttribute(<span class="string">"typeHandler"</span>);</span><br><span class="line">    <span class="comment">// 先获取各个属性</span></span><br><span class="line">    <span class="comment">// 取得 javaType 对应的类型</span></span><br><span class="line">    Class&lt;?&gt; javaTypeClass = resolveClass(javaType);</span><br><span class="line">    <span class="comment">// 取得 typeHandler 对应的类型</span></span><br><span class="line">    <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line">    Class&lt;? extends TypeHandler&lt;?&gt;&gt; typeHandlerClass = (Class&lt;? extends TypeHandler&lt;?&gt;&gt;) resolveClass(typeHandler);</span><br><span class="line">    <span class="comment">// 取得 jdbcType 对应的类型</span></span><br><span class="line">    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);</span><br><span class="line">    <span class="comment">// 创建 discriminatorMap， 并遍历子节点， 以 value-&gt;resultMap 的方式放入discriminatorMap中</span></span><br><span class="line">    Map&lt;String, String&gt; discriminatorMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    <span class="keyword">for</span> (XNode caseChild : context.getChildren()) &#123;</span><br><span class="line">      String value = caseChild.getStringAttribute(<span class="string">"value"</span>);</span><br><span class="line">      String resultMap = caseChild.getStringAttribute(<span class="string">"resultMap"</span>, processNestedResultMappings(caseChild, resultMappings));</span><br><span class="line">      discriminatorMap.put(value, resultMap);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 创建鉴别器</span></span><br><span class="line">    <span class="keyword">return</span> builderAssistant.buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>在鉴别器内部中，也是含有ResultMapping的：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Discriminator</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> ResultMapping resultMapping;</span><br><span class="line">  <span class="keyword">private</span> Map&lt;String, String&gt; discriminatorMap;</span><br><span class="line">  ......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>创建ResultMap对象</p>
<p>解析各个属性和子节点完毕之后，创建ResultMapResolver对象，通过对象可以生成ResultMap。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 创建并添加 ResultMap 到 Configuration 对象中</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> id id, 配置了 id 可以提高效率</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> type 类型</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> extend 继承</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> discriminator 鉴别器</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> resultMappings 列集</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> autoMapping 是否自动映射</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@return</span> 返回创建的 ResultMap 对象</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> ResultMap <span class="title">addResultMap</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">      String id,</span></span></span><br><span class="line"><span class="function"><span class="params">      Class&lt;?&gt; type,</span></span></span><br><span class="line"><span class="function"><span class="params">      String extend,</span></span></span><br><span class="line"><span class="function"><span class="params">      Discriminator discriminator,</span></span></span><br><span class="line"><span class="function"><span class="params">      List&lt;ResultMapping&gt; resultMappings,</span></span></span><br><span class="line"><span class="function"><span class="params">      Boolean autoMapping)</span> </span>&#123;</span><br><span class="line">    id = applyCurrentNamespace(id, <span class="keyword">false</span>);</span><br><span class="line">    extend = applyCurrentNamespace(extend, <span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (extend != <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (!configuration.hasResultMap(extend)) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IncompleteElementException(<span class="string">"Could not find a parent resultmap with id '"</span> + extend + <span class="string">"'"</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 从 configuration 中获取继承的结果集</span></span><br><span class="line">      ResultMap resultMap = configuration.getResultMap(extend);</span><br><span class="line">      <span class="comment">// 获取所集成结果集的所有 ResultMapping 集合</span></span><br><span class="line">      List&lt;ResultMapping&gt; extendedResultMappings = <span class="keyword">new</span> ArrayList&lt;&gt;(resultMap.getResultMappings());</span><br><span class="line">      <span class="comment">// 移除需要覆盖的 ResultMapping 集合</span></span><br><span class="line">      extendedResultMappings.removeAll(resultMappings);</span><br><span class="line">      <span class="comment">// 如果该 resultMap 中定义了构造节点， 则移除其父节点的构造器</span></span><br><span class="line">      <span class="keyword">boolean</span> declaresConstructor = <span class="keyword">false</span>;</span><br><span class="line">      <span class="keyword">for</span> (ResultMapping resultMapping : resultMappings) &#123;</span><br><span class="line">        <span class="keyword">if</span> (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) &#123;</span><br><span class="line">          declaresConstructor = <span class="keyword">true</span>;</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (declaresConstructor) &#123;</span><br><span class="line">        Iterator&lt;ResultMapping&gt; extendedResultMappingsIter = extendedResultMappings.iterator();</span><br><span class="line">        <span class="keyword">while</span> (extendedResultMappingsIter.hasNext()) &#123;</span><br><span class="line">          <span class="keyword">if</span> (extendedResultMappingsIter.next().getFlags().contains(ResultFlag.CONSTRUCTOR)) &#123;</span><br><span class="line">            extendedResultMappingsIter.remove();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 添加需要被继承的 ResultMapping 集合</span></span><br><span class="line">      resultMappings.addAll(extendedResultMappings);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 通过建造者模式创建 ResultMap 对象</span></span><br><span class="line">    ResultMap resultMap = <span class="keyword">new</span> ResultMap.Builder(configuration, id, type, resultMappings, autoMapping)</span><br><span class="line">        .discriminator(discriminator)</span><br><span class="line">        .build();</span><br><span class="line">    <span class="comment">// 添加到 Configuration 对象中</span></span><br><span class="line">    configuration.addResultMap(resultMap);</span><br><span class="line">    <span class="keyword">return</span> resultMap;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<p><a href="https://mp.weixin.qq.com/s/zL0d4odAeZIqTTeOesXgYA" target="_blank" rel="noopener">https://mp.weixin.qq.com/s/zL0d4odAeZIqTTeOesXgYA</a></p>
<h2 id="Github示例代码"><a href="#Github示例代码" class="headerlink" title="Github示例代码"></a>Github示例代码</h2><p> <a href="https://github.com/homejim/mybatis-cn" target="_blank" rel="noopener">https://github.com/homejim/mybatis-cn</a> </p>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://linpaibin.gitee.io/2020/01/15/Mybatis%E4%B8%AD%E7%9A%84resultMap%E8%A7%A3%E6%9E%90/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Mybatis/" rel="tag">Mybatis</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/01/16/Java/Java%E9%87%8D%E5%86%99%E6%AF%94%E8%BE%83%E5%99%A8/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Java重写比较器
          
        </div>
      </a>
    
    
      <a href="/2020/01/12/%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%AE%97%E6%B3%95/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">负载均衡算法</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: 'gtXNe43HVDH72a7XMn9zNgz8-gzGzoHsz',
        app_key: 'Df8WdcqcC2DCWL1pfcWb4QfN',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '既然都来了，便留个爪印呗~_(¦3」∠)_',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2019-2020
        LinPaiBin
      </li>
      <li>
        
          Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar" style="opacity: 0.5;">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="滨书"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E7%94%9F%E6%B4%BB/">生活</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/12/02/AboutMe">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['人生如棋，吾愿为卒','愿你一生努力，一生无憾','不求事事顺意，但求无愧于心'],
    startDelay: 0,
    typeSpeed: 250,
    loop: true,
    backSpeed: 100,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: false
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>




<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>